import {observable, action, computed} from 'mobx';
import * as API_WFREPORT from '../../apis/efficiencyReport';
import * as ReportUtil from "../../util/reportUtil";
import {ReportStore} from "./ReportStore";
import {WeaForm, WeaTableNew} from "comsMobx";
const {TableStore} = WeaTableNew;
/**
 * 人员办理时间分析报表
 */
export  class FlowHandleAnalyseStore extends ReportStore {

    @observable tableStore = new TableStore();
    dataKey = "";
    labels = {};
    @observable advanceSearchPara = {
        orgtype:3,
        orgid:'',
        datarange:1,
        dataid:'',
        dateType: 1,
        startDate: '',
        endDate: '',
        tabKey:'1'
    }

    //modal
    @observable modalPara = {
        modalVisible:false,
        modalKey:'',
        modaltitle:'',
        dataParams:{},
        tabDatas:[]
    }

    @observable exportExcelUrl = '';


    @computed get collectParams(){
        return {
            favouritetype: 2,
            importantlevel:1,
            favname:this.labels.reportTitle,
            link:"wui/engine.html#/main/workflow/flowHandleAnalyse"
        }
    }

    constructor(){
        super();
        this.initDatas  = this.initDatas.bind(this);
        this.doSearch  = this.doSearch.bind(this);
        this.updateAdvanceSearchPara = this.updateAdvanceSearchPara.bind(this);
        this.showModal = this.showModal.bind(this);
        this.exportExcel = this.exportExcel.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
    }

    @action
    initDatas(){
        API_WFREPORT.getFlowHandleSearchCondition({}).then((data)=>{
            const {noRight = false,rightKey = '-100',labels = {},tabDatas = [] ,config = [],reportDetailTabs = [],bottomInfo,reportid} = data;
            this.advanceSearchData  = {noRight,tabDatas,config,reportDetailTabs,bottomInfo,reportid};
            if(!noRight){
                this.labels  = labels;
                this.updateAdvanceSearchPara({rightKey,tabKey:1});
            }
        });
    }

    doSearch(){
        this.loading = true;
        API_WFREPORT.getFlowHandleReportData(this.advanceSearchPara).then((data)=>{
            this.tableStore.getDatas(data.sessionkey, 1);
            this.dataKey = data.sessionkey;
            this.loading = false;
        })
    }

    updateAdvanceSearchPara(para){
        let isload = true;
        let defaultInfo = {};
        let iddoupdate = false;
        for(let o in para){
            if(this.advanceSearchPara[o] != para[o]){
                iddoupdate = true;
                if(o === 'orgtype'){
                    const orgtype = para[o];
                    if(orgtype > 1){
                        const {config = [] } = this.advanceSearchData;
                        if(config.length > 0){
                            const {selectLinkageDatas = {}}  =  config[0];
                            defaultInfo.orgid = ReportUtil.getDefaultId(orgtype,selectLinkageDatas);
                        }
                    }
                }
                if(o === 'datarange' && para[o] > 1) isload = false;
                if(o === 'datarange' && para[o] == 1) {
                    defaultInfo.dataid = '';
                }
                if(o === 'orgid' && para[o] == '') isload  = false;
                if(o === 'dataid' && para[o] == '' && (para.datarange > 1 || (para.datarange > 1 && this.advanceSearchPara.datarange > 1))){
                    isload  = false;
                }
                if(o === 'dateType' ){
                    if(para[o] == 6){
                        isload = false;
                    }else{
                        defaultInfo.startDate = '';
                        defaultInfo.endDate = '';
                    }
                }
            }
        }
        if(iddoupdate){
            this.advanceSearchPara= {...this.advanceSearchPara, ...para,...defaultInfo};
            if(isload){
                this.doSearch();
            }
        }
    }

    showModal(record = {},column) {
        const {id} = record;
        const {reportDetailTitle} = this.labels;
        const {reportDetailTabs} = this.advanceSearchData;
        const dataParams = {
            'api' : 'getFlowHandleRequestList',
            'keyid':id,
            'column':column,
            ...this.advanceSearchPara
        }
        this.modalPara = {
            modalVisible:true,
            modaltitle:reportDetailTitle,
            dataParams:dataParams,
            tabDatas:reportDetailTabs,
            needAdvacnceSearch:true
        }
    }

    exportExcel() {
       const {reportTitle = ''} = this.labels
        // this.exportExcelUrl = '/spa/workflowReport/exportExcel.jsp?tableKey=' + this.dataKey + '&filename=' + reportTitle +'&_t=' + (new Date().getTime());
        const api='FlowHandleAnalyse';
        this.exportExcelUrl  = encodeURI('/api/report/workflow/exportExcel?tableKey='+this.dataKey+'&filename='+reportTitle+'&_t='+(new Date().getTime())+'&api='+api);
    }

    clearStatus(){
        this.loading = false;
        this.dvanceSearchPara = {
            orgtype:3,
            orgid:'',
            datarange:1,
            dataid:'',
            dateType: 1,
            startDate: '',
            endDate: '',
            tabKey:'1'
        }
        this.dataKey = '';
        this.tableStore = new TableStore();
        this.modalPara = {};
        this.exportExcelUrl = '';
        this.advanceSearchData = {};
        this.analyseData = {};
    }
}